import { API_LIST } from '../../../Api/Api'
import { commonTableList, BuiltInData } from '../Common/common'
import {
    AddDataButton,
    ModDataButton,
    DelDataButton,
    MoreActionsButton,
    AnnexButton,
    OpenExcelButton,
    OpenSelection,
} from '../Common/options'
import { CameraPositionTree, ProjectList, GetIPBroadcast } from '../Common/Conversion'

import { project } from '../../../Data'
const { hostname } = window.location
const { client_id } = project[hostname]
const out = ['bada', 'jtgj']

export const Camera = async ({ url, OptionsEvent, ClientData, Permission, actionRef }) => {
    let data = {
        pageSize: 10,
        params: { pageIndex: 1, OSortId: 'asc@1' },
        get: API_LIST()[`get${url}`],
        added: `pos${url}`,
        modify: `put${url}`,
        delete: `del${url}`,
        options: [
            !out.includes(client_id) && (
                <OpenExcelButton
                    key="one"
                    title="置顶管理"
                    OptionsEvent={OptionsEvent}
                    data={{
                        save: true,
                        submit: true,
                        url: 'getCameraPinTopInfo',
                        pos: 'posUploadCameraPinTopInfo',
                    }}
                />
            ),
            (Permission.findIndex(v => v.uuid === 'dfd46283-0a71-40e9-a882-00ed0ead5634') !== -1 ||
                Permission.findIndex(v => v.uuid === '125e5c18-395d-4813-8a2b-572d9f1abbec') !== -1 ||
                Permission.findIndex(v => v.uuid === '418fd14e-2383-47b8-b740-70153c9d90a8') !== -1) && (
                <MoreActionsButton
                    title="更多操作"
                    type="primary"
                    data={{
                        actionRef,
                        OptionsEvent,
                        CameraEnable: { type: 'ExecuteButton', url: 'posCameraUpdateIsEnabled', tips: '设备全部启用' },
                        CameraDisable: {
                            type: 'ExecuteButton',
                            url: 'posCameraUpdateIsNotEnabled',
                            tips: '设备全部禁用',
                        },
                        CameraOffLine: {
                            type: 'ExecuteButton',
                            url: 'posCameraUpdateIsEnabledByOnline',
                            tips: '离线设备已禁用',
                        },
                        CameraStatus: { type: 'ExecuteButton', url: 'posCameraStatusSync', tips: '同步在线状态' },
                        // CameraCloudSync: { type: 'ExecuteButton', url: 'posCameraCloudSync', tips: '同步萤石云状态' },
                    }}
                    items={[
                        Permission.findIndex(v => v.uuid === 'dfd46283-0a71-40e9-a882-00ed0ead5634') !== -1 && {
                            key: 'CameraEnable',
                            label: '全部启用',
                        },
                        Permission.findIndex(v => v.uuid === '125e5c18-395d-4813-8a2b-572d9f1abbec') !== -1 && {
                            key: 'CameraDisable',
                            label: '全部禁用',
                        },
                        Permission.findIndex(v => v.uuid === '418fd14e-2383-47b8-b740-70153c9d90a8') !== -1 && {
                            key: 'CameraOffLine',
                            label: '禁用离线设备',
                        },
                        // {
                        //     key: 'CameraCloudSync',
                        //     label: '同步摄像头到萤石云',
                        // },
                        {
                            key: 'CameraStatus',
                            label: '同步在线状态',
                        },
                    ]}
                />
            ),

            //
            Permission?.findIndex(v => v.uuid === '4313085f-672d-4607-8d40-9cbdd24b4464') !== -1 && (
                <AddDataButton OptionsEvent={OptionsEvent} type="primary" icon="add" title="新增" />
            ),
            <OpenSelection
                OptionsEvent={OptionsEvent}
                title="批量删除"
                type="primary"
                danger
                data={{
                    type: 'SelectionDelete',
                    url: 'posCameraBatchRemoveCamera',
                }}
            />,
        ],
        columns: [
            commonTableList.uuid,
            { ...commonTableList.SearchSort, selectType: 'Camera' },
            {
                ...commonTableList.projectUuid,
                valueEnum: await ProjectList(),
                hideInAdded: true,
                hideInModify: true,
                hideInTable: true,
                hideInSearch: !hostname.includes('jszx'),
            },
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            { ...commonTableList.tableName, title: '摄像头名称', width: 240 },
            {
                dataIndex: 'cameraFactory',
                title: '摄像头品牌',
                valueType: 'select',
                valueEnum: BuiltInData.camerafactory,
                rules: true,
                width: 120,
            },
            {
                dataIndex: 'deviceSerial',
                title: '设备序列号',
                valueType: 'input',
                width: 150,
            },
            {
                dataIndex: 'channelNo',
                title: '通道号',
                valueType: 'input',
                hideInSearch: true,
                width: 100,
            },
            {
                dataIndex: 'appid',
                title: 'Appid',
                valueType: 'input',
                hideInSearch: true,
                width: 250,
            },
            {
                dataIndex: 'ptz',
                title: '云台控制',
                valueType: 'select',
                valueEnum: BuiltInData.CameraPTZ,
                rules: true,
                width: 100,
            },
            {
                dataIndex: 'talk',
                title: '语音对讲',
                valueType: 'select',
                valueEnum: BuiltInData.isYes,
                rules: true,
                width: 100,
            },
            {
                dataIndex: 'hd',
                title: '高清画面',
                valueType: 'select',
                valueEnum: BuiltInData.isYes,
                rules: true,
                width: 100,
            },
            {
                dataIndex: 'isAlarm',
                title: '启用预警',
                valueType: 'select',
                valueEnum: BuiltInData.isYes,
                rules: true,
                width: 100,
            },
            {
                dataIndex: 'playUrl',
                title: '直播地址',
                valueType: 'input',
                ellipsis: true,
                width: 200,
            },
            {
                dataIndex: 'cameraPositionUuid',
                title: '摄像头位置',
                valueType: 'treeSelect',
                fieldProps: {
                    options: await CameraPositionTree(),
                    fieldNames: { label: 'tableName', value: 'uuid' },
                },
                rules: true,
                width: 150,
            },
            {
                dataIndex: 'cameraTypeUuid',
                title: '摄像头类别',
                valueType: 'select',
                valueEnum: ClientData.cameraType,
                rules: true,
                width: 150,
            },
            {
                dataIndex: 'online',
                title: '在线状态',
                valueType: 'select',
                valueEnum: BuiltInData.online,
                rules: true,
                width: 100,
            },
            { ...commonTableList.isEnabled, rules: true },
            {
                dataIndex: 'lng',
                title: '经度',
                valueType: 'input',
                hideInSearch: true,
                width: 150,
            },
            {
                dataIndex: 'lat',
                title: '纬度',
                hideInSearch: true,
                valueType: 'input',
                width: 150,
            },
            { ...commonTableList.sortId, rules: true, hideInSearch: true },
            {
                dataIndex: 'simId',
                title: 'SIM卡号',
                hideInSearch: true,
                valueType: 'input',
                width: 150,
            },
            {
                dataIndex: 'synced',
                title: '是否同步',
                hideInSearch: true,
                valueType: 'select',
                rules: true,
                valueEnum: BuiltInData.isYes,
                width: 150,
            },
            {
                dataIndex: 'SIMIdImgAttachmentUuid',
                title: 'SIM图片',
                valueType: 'upload',
                valueFile: [1],
                fileUrl: 'fileUrl',
                hideInSearch: true,
                hideInTable: true,
            },
            {
                dataIndex: 'ipBroadcastUuid',
                title: '其他设备',
                hideInSearch: true,
                valueType: 'select',
                valueEnum: await GetIPBroadcast(),
                width: 150,
            },
            {
                dataIndex: 'fileUrl',
                title: 'SIM图片',
                valueType: 'hidden',
                hideInSearch: true,
                hideInTable: true,
                width: 400,
            },
            !out.includes(client_id) && {
                dataIndex: 'isPinTop',
                title: '是否置顶',
                valueType: 'select',
                valueEnum: BuiltInData.isYes,
                width: 100,
            },
            !out.includes(client_id) && {
                dataIndex: 'pinTopSortId',
                title: '置顶顺序',
                valueType: 'input',
                width: 100,
                hideInSearch: true,
            },
            commonTableList.remark,
            commonTableList.createUserName,
            commonTableList.createTime,
            commonTableList.updateUserName,
            commonTableList.updateTime,
            {
                title: '操作',
                valueType: 'systemOperation',
                fixed: 'right',
                width: 240,
                hideInSearch: true,
                hideInAdded: true,
                hideInModify: true,
                render: (_, record) => {
                    const { simIdImgUrl } = record
                    return [
                        Permission.findIndex(v => v.uuid === 'bf4a780f-6d6e-494d-9ba6-20d44bf2605f') !== -1 && (
                            <ModDataButton
                                OptionsEvent={OptionsEvent}
                                key="modify"
                                title="修改"
                                url={url}
                                data={record}
                            />
                        ),
                        simIdImgUrl && (
                            <AnnexButton OptionsEvent={OptionsEvent} key="see" url={simIdImgUrl} title="SIM卡" />
                        ),
                        Permission.findIndex(v => v.uuid === 'd13aeabf-0f16-462f-b9bb-32e878e824b8') !== -1 && (
                            <DelDataButton
                                OptionsEvent={OptionsEvent}
                                key="del"
                                title="删除"
                                url={url}
                                uuid={record.uuid}
                            />
                        ),
                    ]
                },
            },
        ],
    }
    return data
}
